<template>
  <PageTemplate rowKey="affairId"
                :tableRequest="tableRequest"
                :metaDataRequestParams="metaDataRequestParams"
                :toolButtonList="toolButtonList"
                :tableSlotNames="tableSlotNames"
                @onMetaDataChange="handleMetaDataChange"
                @onToolClick="handleToolClick"
                @onRowClick="handleRowClick">
    <component v-for="slotItem in tableSlots"
               :key="slotItem.componentId"
               :is="slotItem.componentId"
               :slot="slotItem.name"
               slot-scope="{text,record,resetTableData}"
               v-bind="slotItem.getAttrs && slotItem.getAttrs(record, slotItem)"
               @click.stop="handleSlotItemClick(slotItem, record, resetTableData)">
      {{ slotItem.showText === false ? '' : slotItem.format ?
        slotItem.format(text) :
        text
      }}</component>
  </PageTemplate>
</template>
<script>
  import PageTemplate from '@/components/page-template';
  import { takeBack, deleteItems, informItem } from '@/utils/workflow.js';
  import {
    transmitColFromGrid,
    transmitMail,
    showFlowChartAJax,
    setTrack,
    tempShowDetailLogDialog,
    getParams
  } from '@/utils';
  import collaboration from '@/utils/collaboration.js';
  import mixin from '../mixin';
  import TableCollum from './table-collum.vue';
  import smartCodeMixin from '@/mixins/smart-code-mixin.js';

  const urlParamObj = getParams(window.location.search);
  export default {
    name: 'PageAllDownList',
    mixins: [mixin, smartCodeMixin],
    components: { PageTemplate, TableCollum },
    data() {
      return {
        tableSlots: [
          {
            name: 'subject',
            componentId: 'TableCollum',
            getAttrs(record, { name }) {
              return { record, type: name };
            }
          },
          {
            name: 'nodeDeadLineName',
            componentId: 'TableCollum',
            getAttrs(record, { name }) {
              return { record, type: name };
            }
          },
          {
            name: 'currentNodesInfo',
            componentId: 'a',
            getAttrs({ currentNodesInfo }) {
              return {
                title: currentNodesInfo
              };
            }
          },
          {
            name: 'isTrack',
            componentId: 'a',
            format: this.isTrue
          },
          {
            name: 'affairArchiveId',
            componentId: 'span',
            format: this.isTrue
          },
          {
            name: 'processId',
            componentId: 'a',
            showText: false,
            getAttrs() {
              return {
                class: 'xicon-form-text',
                style: 'color:#1F85EC'
              };
            }
          }
        ],
        metaDataRequestParams: {
          key: 'LIST_DONE',
          platform: 0
        },
        toolButtonList: []
      };
    },
    computed: {
      tableRequest() {
        return {
          url: '/ajax.do?method=ajaxAction&managerName=colManager',
          managerMethod: 'getDoneList',
          otherArgs: {
            dumpData: this.dataType === '1' ? 'true' : 'false',
            templeteIds: urlParamObj.textfield || ''
          }
        };
      },
      tableSlotNames() {
        return this.tableSlots.map((slot) => slot.name);
      }
    },
    methods: {
      isTrue(value) {
        return Boolean(value) ? this.$i18n('common.yes') : this.$i18n('common.no');
      },
      //按钮点击事件
      handleToolClick(buttonId, { selectedRows, metaData, searchConditionsValue }, resetTableData) {
        switch (buttonId) {
          case 'transmitCol':
            transmitColFromGrid(
              selectedRows,
              resetTableData,
              null,
              metaData.globalParam.isSecretLevelEnable
            );
            break;
          case 'transmitMail':
            transmitMail(selectedRows, resetTableData);
            break;
          case 'pigeonhole':
            collaboration.doPigeonhole(selectedRows, resetTableData, 'done');
            break;
          case 'delete':
            deleteItems(selectedRows, resetTableData);
            break;
          case 'takeBack':
            takeBack(selectedRows, resetTableData);
            break;
          case 'aiProcessing':
            this.aiProcessing = value;
            this.searchTableData();
            break;
          case 'deduplication':
            this.deduplication = value;
            this.searchTableData();
            break;
          case 'smartCodeScan':
            this.searchConditionsValue = searchConditionsValue;
            this.showSmartCode && this.showSmartCode();
            break;
          case 'currentData':
            this.currentDataClickHandle(resetTableData);
            break;
          case 'dumpData':
            this.dumpDataClickHandle(resetTableData);
            break;
          case 'inform'://已办事项新增“知会”
            informItem(selectedRows, resetTableData);
            break;
        }
      },
      handleSlotItemClick(slotItem, record, resetTableData) {
        switch (slotItem.name) {
          case 'currentNodesInfo':
            let showHastenButton = false;
            const {
              affairId,
              caseId,
              processId,
              templeteId,
              activityId,
              bodyType,
              startMemberId,
              memberId
            } = record;
            if (startMemberId == memberId) {
              showHastenButton = true;
            }
            showFlowChartAJax(
              affairId,
              caseId,
              processId,
              templeteId,
              activityId,
              bodyType,
              showHastenButton,
              this.dataType
            );
            break;
          case 'isTrack':
            setTrack(record, resetTableData);
            break;
          case 'processId':
            tempShowDetailLogDialog(
              record.summaryId,
              record.processId,
              2,
              this.dataType,
              record.affairId
            );
            break;
        }
        console.log(slotItem, resetTableData);
      },
      handleMetaDataChange({ globalParam }) {
        const needButtonList = [
          ['transmit', ['transmitCol', 'transmitMail']],
          'pigeonhole',
          'delete',
          'takeBack',
          'aiProcessing',
          'deduplication','inform'
        ];
        this.supportSmartCode = globalParam ? globalParam.supportSmartCode : undefined;
        let smartCodeIndex = 4;
        if (!this.isHaveSmartCodePermission()) {
          // 没有扫一扫权限
          smartCodeIndex = undefined;
        }
        this.initToolButtonList(needButtonList, globalParam, smartCodeIndex);
      },
      // 智码扫码成功后，智码预处理时，获取解码参数
      getSmartCodeDecodeParam(data) {
        const obj = this.searchConditionsValue;
        obj.openFrom = 'listDone';
        return obj;
      },
      // 智码扫码成功后回调
      smartCodeCallBack(data) {
        collaboration.smartCodeCallBack(data);
      }
    }
  };
</script>
